package com.epoint.utils.template.update;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.epoint.utils.JDBCUtil;
import com.epoint.utils.SQLUtil;

/**
 * 我的更新模板
 * @author 梁卓
 *
 * @param <T> 泛型
 */
public class MySqlUpdate<T> extends UpdateTemplate<T>
{

    private Connection con = null;
    private PreparedStatement ps = null;
    private int i = 0;

    @Override
    void getConnection() {
        this.con = JDBCUtil.getConnection();
        
    }

    /**
         * 执行sql返回结果
     * @param sql sql
     * @param sqlFieldList 更新时sql字段的顺序
     * @param obj 要更新的对象信息 
     * @return 结果
     */
    @Override
    int updateData(String sql,List<String> sqlFieldList, T obj) {
        try {
            this.ps = this.con.prepareStatement(sql);
            this.ps = SQLUtil.updateObjectInfo(obj, sqlFieldList, ps);
            System.out.println("MySqlInsert->" + this.ps);
            this.i = this.ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改模板错误！");
        }
        return i;
    }

    @Override
    void closeConnection() {
        try {
            JDBCUtil.closeConnection(null, this.ps, this.con);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改模板连接关闭失败！");
        }
    }

}
